package addon.w3.w3x;

import java.io.IOException;

public class W3XDoodad extends W3XCom {
	public int version;
	public int numTree;
	public W3XTreeData treeData[];
	
	public float min_coordX;
	public float min_coordY;
	public float min_coordZ;
	
	public float max_coordX;
	public float max_coordY;
	public float max_coordZ;
	
	public W3XDoodad(byte data[]) {
		super(data);
	}

	@Override
	public void _decode() throws W3XException, IOException {
		byte id[] = new byte[4];
		lein.read(id);
		if(!new String(id).equals("W3do")) {
			throw new W3XException("Invalid w3do tag="+new String(id));
		}
		version = lein.readInt();
		lein.readInt(); //unknown
		numTree = lein.readInt();
		
		//debug("vers : "+version +"      numTree:"+numTree);
		
		if(version != 8) throw new W3XException("Can't support this version ("+version+"). Only suppor ver8");
		
		treeData = new W3XTreeData[numTree];
		for(int i=0;i<numTree;i++) {
			treeData[i] = new W3XTreeData(lein);
			treeData[i].decode();
		}
		
		for(int i=0;i<numTree;i++) {
			if( min_coordX > treeData[i].coordX ) {
				min_coordX = treeData[i].coordX;
			} else if( max_coordX < treeData[i].coordX ) {
				max_coordX = treeData[i].coordX;
			}
			
			if( min_coordY > treeData[i].coordY ) {
				min_coordY = treeData[i].coordY;
			} else if( max_coordY < treeData[i].coordY ) {
				max_coordY = treeData[i].coordY;
			}
			
			if( min_coordZ > treeData[i].coordZ ) {
				min_coordZ = treeData[i].coordZ;
			} else if( max_coordZ < treeData[i].coordZ ) {
				max_coordZ = treeData[i].coordZ;
			}
		}
		
//		System.out.println("min_cx:"+min_coordX);
//		System.out.println("max_cx:"+max_coordX);
//		
//		System.out.println("min_cy:"+min_coordY);
//		System.out.println("max_cy:"+max_coordY);
//		
//		System.out.println("min_cz:"+min_coordZ);
//		System.out.println("max_cz:"+max_coordZ);
	}
	
	/**
	 * Header:
char[4]: file ID = "W3do"
int: file version = 8
int: unknown ([0B 00 00 00]h)
int: number of trees defined
Data
Each tree is defined by a block of 50 bytes organized like this:
char[4]: Tree ID (can be found in the file "Units\DestructableData.slk")
int: Variation (little endian)
float: Tree X coordinate on the map
float: Tree Y coordinate on the map
float: Tree Z coordinate on the map
float: Tree angle (radian angle value)(degree = radian*180/pi)
float: Tree X scale
float: Tree Y scale
float: Tree Z scale
byte: Tree flags*
byte: Tree life (integer stored in %, 100% is 0x64, 170% is 0xAA for example)
int: unknown (-1)
int: unknown (0)
int: Tree ID number in the World Editor (little endian) (each tree has a different one)

*flags:
0= invisible and non-solid tree
1= visible but non-solid tree
2= normal tree (visible and solid)

To sum up how it looks:
tt tt tt tt vv vv vv vv xx xx xx xx yy yy yy yy zz zz zz zz aa aa aa aa xs xs xs xs ys ys ys ys zs 
zs zs zs ff ll bb bb bb bb cc cc cc cc dd dd dd dd
where:
tt: type
vv: variation
xx: x coordinate
yy: y coordinate
zz: z coordinates
aa: rotation angle
xs: x scale
ys: y scale
zs: z scale
ff: flags
ll: life
bb: unknown
cc: unknown
dd: doodad number in the editor

	 */
}
